/*
Package verifycode
@Author   : Uyynot
@Email    : uyynot@qq.com
@Time     : 2024/2/22 18:22
@File     : store_redis.go
@Project  : gohub
@Desc     :
*/
package verifycode

import (
	"gohub/pkg/app"
	"gohub/pkg/config"
	"gohub/pkg/redis"
	"time"
)

// RedisStore 实现verifycode.Store接口
type RedisStore struct {
	RedisClient *redis.RedisClient
	KeyPrefix   string
}

// Set 实现verifycode.Store接口的Set方法
func (s *RedisStore) Set(key, value string) bool {
	//过期时间
	ExpireTime := time.Minute * time.Duration(config.GetInt64("verifycode.expire_time"))

	//本地环境调试
	if app.IsLocal() {
		ExpireTime = time.Minute * time.Duration(config.GetInt64("verifycode.debug_expire_time"))
	}

	return s.RedisClient.Set(s.KeyPrefix+key, value, ExpireTime)
}

// Get 实现verifycode.Store接口的Get方法
func (s *RedisStore) Get(key string, clear bool) string {
	value := s.RedisClient.Get(s.KeyPrefix + key)
	if clear {
		s.RedisClient.Del(s.KeyPrefix + key)
	}
	return value

}

// Verify 实现verify.Store接口的Verify方法
func (s *RedisStore) Verify(key, answer string, clear bool) bool {
	value := s.Get(key, clear)
	return value == answer

}
